package hy.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Component
public class AsyncTaskExecutor {
    @Autowired
    KafkaTemplate<String,Msg> tmp;
    @Async
    @Transactional
    public void executor()throws Exception {
        while (true) {
            Thread.sleep(1000);
            Msg m=new Msg();
            m.setContent(UUID.randomUUID().toString());
            System.out.println(Thread.currentThread().getName());
            tmp.send("first",m);
        }
    }
    @Async
    @KafkaListener(topics="first",groupId = "gr01")
    public void consumMsg(Msg msg){
        System.out.println("接收者："+Thread.currentThread().getName());
        System.out.println("================"+msg+"==============");
    }
}